package replaceWords;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class WeiYuanChuangXML {

	public void wyc() throws Exception {
		final String wordSpilt = "|";
		String contentNoMark = null;
		String wordSd = null;
		String wordReplace = null;
		String wordReplaceNoMark = null;
		Complex complexDiv = new Complex();
		MarkDealXML md = new MarkDealXML();
		md.setWordDBName(wordDBName);
		md.setWordHost(wordHost);
		md.setMarkList(markList);
		md.setMarkSuccedaneumsList(markSuccedaneumsList);
		ReplaceWordXML replaceword = new ReplaceWordXML();
		replaceword.setWordDBName(wordDBName);
		replaceword.setWordHost(wordHost);
		replaceword.setNewonesList(newonesList);
		replaceword.setWordsList(wordsList);

		// 去掉标点符号
		contentNoMark = md.markdel(content);
		// System.out.println(contentNoMark);
		// 分词

		wordSd = complexDiv.segWords(contentNoMark, wordSpilt);
		// System.out.println(wordSd);
		// 替换
		wordReplace = replaceword.replaceNum(replaceword.replaceWord(wordSd));
		// 去掉|
		wordReplaceNoMark = wordReplace.replaceAll("\\|", "");
		// 恢复标点
		wordOK = md.markAdd(wordReplaceNoMark);
		// }
	}
	
	//在构造方法里读取替换词库和替换标点符号库
	public WeiYuanChuangXML(){
		String xmlpath = "D:\\MyTool\\wordsWYC.xml";
		String xmlpath2 = "D:\\MyTool\\markList.xml";
		SAXBuilder builder = new SAXBuilder(false);
		try {
			Document doc = builder.build(xmlpath);
			Element wordsElement = doc.getRootElement();
			List rowslist = wordsElement.getChildren("row");
			for (Iterator iter = rowslist.iterator(); iter.hasNext();) {
				Element row = (Element)iter.next();
				List fieldList = row.getChildren("field");
				for (Iterator iterField = fieldList.iterator(); iterField.hasNext();){
					Element field = (Element)iterField.next();
					String fieldName = field.getAttributeValue("name");
					if(fieldName=="word"){
						String word = field.getText();
						wordsList.add(word);
					}else{
						String newone = field.getText();
						newonesList.add(newone);
					}
				}
			}
			
			Document doc2 = builder.build(xmlpath2);
			Element wordsElement2 = doc2.getRootElement();
			List rowslist2 = wordsElement2.getChildren("row");
			for (Iterator iter2 = rowslist2.iterator(); iter2.hasNext();) {
				Element row2 = (Element)iter2.next();
				List fieldList2 = row2.getChildren("field");
				for (Iterator iterField2 = fieldList2.iterator(); iterField2.hasNext();){
					Element field2 = (Element)iterField2.next();
					String fieldName2 = field2.getAttributeValue("name");
					if(fieldName2=="mark"){
						String word2 = field2.getText();
						markList.add(word2);
					}else{
						String newone2 = field2.getText();
						markSuccedaneumsList.add(newone2);
					}
				}
			}
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	List<String> wordsList = new ArrayList();
	List<String> newonesList = new ArrayList();
	List<String> markList = new ArrayList();
	List<String> markSuccedaneumsList = new ArrayList();
	String wordHost = null;
	String wordDBName = null;
	String content = null;
	
	public void setContent(String content) {
		this.content = content;
	}

	String wordOK = null;

	public String getWordOK() {
		return wordOK;
	}

	public void setWordHost(String wordHost) {
		this.wordHost = wordHost;
	}

	public void setWordDBName(String wordDBName) {
		this.wordDBName = wordDBName;
	}
}
